package com.gxuwz.medical.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.math.NumberUtils;

import com.gxuwz.medical.dao.PolicyDao;
import com.gxuwz.medical.domain.policy.Policy;
/**
 * 
 * @author 演示
 *
 */
public class PolicyServlet extends BaseServlet {

	PolicyDao policyDao=new PolicyDao();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String m = req.getParameter("m");// 请求处理动作类型:list:显示列表；get：根据ID读取记录;input:新增数据;add:保存新记录；edit：更新记录
		String url = ERROR_PATH;
		try {
			if ("list".equals(m)) {	
				url="/page/policy/policy_list.jsp";
			} else if ("input".equals(m)) {
				url="/page/policy/policy_add.jsp";
			} else if ("get".equals(m)) {
				url="/page/policy/policy_edit.jsp";
			} else if ("add".equals(m)) {
				url = this.add(req, resp);
			} else if("allowed".equals(m)){
				url = this.setup(req, resp, "1");
			}else if("unallowed".equals(m)){
				url = this.setup(req, resp, "0");
			}else if ("del".equals(m)) {
				del(req, resp);
			}else{
				error(req, resp);
			}  
		} catch (Exception e) {
			e.printStackTrace();
		}
		  process(req, resp, url);

	}
	private String add(HttpServletRequest req,HttpServletResponse resp)throws Exception{

	  	  //1：接收参数
	  	  String runyear=req.getParameter("runyear");
	  	  String maxlinestr=req.getParameter("maxline");
	  	  String status=req.getParameter("status");
	  	  String remark=req.getParameter("remark");
	  	  double maxline=0.00;
	  	  if(NumberUtils.isNumber(maxlinestr)){
	  		 maxline =Double.parseDouble(maxlinestr);
	  	  }
	  	  //2:构造对象
	  	  Policy model=new Policy(runyear, maxline, status, remark);
	  	  //3：调用保存的方法
	  	  try{
	  		  model.add();
	  		  return "/page/policy/policy_list.jsp";
	  	  }catch(Exception e){
	  		 throw e;
	  	  }
	  	  
		}
	
	private String setup(HttpServletRequest req,HttpServletResponse resp,String status)throws Exception{

	  	  //1：接收参数
	  	  String runyear=req.getParameter("id");
	  	  //2:构造对象
	      Policy model=new Policy(runyear);
	      model.setup(status);
	  	  return "/page/policy/policy_list.jsp";
	  	  
		}
	private void del(HttpServletRequest request, HttpServletResponse response) {
    	Object[] params= new Object[1];
		params[0]=request.getParameter("id");
		//2:调用方法
		try{
			this.policyDao.editOrdelObject("delete from t_policy where runyear=?", params);
			process(request, response, "/page/policy/policy_list.jsp");
		}catch(Exception e){
			e.printStackTrace();
		}	
	}
}
